﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ranet.Olap.Core.DataDescription;
using Galaktika.BI.Silverlight.Controls.PivotGrid.Data;
using System.Collections.Generic;

namespace Galaktika.BI.Silverlight.Controls.PivotGrid.View
{
    public class CellView
    {
        public readonly CellDescription CellDescr = null;
        public readonly MemberInfo ColumnMember = null;
        public readonly MemberInfo RowMember = null;
        public readonly IList<MemberInfo> InvisibleCoords = null;

        public CellView(CellDescription cell_descr, MemberInfo column, MemberInfo row, IList<MemberInfo> invisibleCoords)
        {
            if (cell_descr == null)
            {
                throw new ArgumentNullException("cell_descr");
            }
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }
            if (row == null)
            {
                throw new ArgumentNullException("row");
            }
            if (invisibleCoords == null)
            {
                throw new ArgumentNullException("invisibleCoords");
            }

            CellDescr = cell_descr;
            ColumnMember = column;
            RowMember = row;
            InvisibleCoords = invisibleCoords;
        }

        public bool IsUpdateable
        {
            get
            {
                if (this.HasCustomRollupInTuple == false && this.CellDescr.Value != null && this.CellDescr.Value.CanUpdate)
                    return true;
                return false;
            }
        }

        IDictionary<String, MemberInfo> tuple = null;
        String tupleToStr = String.Empty;
        String shortTupleToStr = String.Empty;

        /// <summary>
        /// Возвращает тапл для ячейки. Ключ: имя иерархии; Значение: элемент
        /// </summary>
        /// <returns></returns>
        public IDictionary<String, MemberInfo> GetTuple()
        {
            if (tuple == null)
            {
                tuple = new Dictionary<String, MemberInfo>();
                if (this.ColumnMember != null)
                {
                    if (!tuple.ContainsKey(this.ColumnMember.HierarchyUniqueName))
                        tuple.Add(this.ColumnMember.HierarchyUniqueName, this.ColumnMember);
                    this.ColumnMember.CollectAncestors(tuple);
                }
                if (this.RowMember != null)
                {
                    if (!tuple.ContainsKey(this.RowMember.HierarchyUniqueName))
                        tuple.Add(this.RowMember.HierarchyUniqueName, this.RowMember);
                    this.RowMember.CollectAncestors(tuple);
                }
                
                foreach (MemberInfo mi in this.InvisibleCoords)
                {
                    if (!tuple.ContainsKey(mi.HierarchyUniqueName))
                        tuple.Add(mi.HierarchyUniqueName, mi);
                }
            }
            return tuple;
        }

        private bool HasCustomRollupInTuple
        {
            get
            {
                bool hasRollup = false;
                IList<MemberInfo> tuple = new List<MemberInfo>();
                if (this.RowMember != null)
                {
                    tuple.Add(this.RowMember);
                    this.RowMember.CollectAncestors(tuple);
                }
                if (this.ColumnMember != null)
                {
                    tuple.Add(this.ColumnMember);
                    this.ColumnMember.CollectAncestors(tuple);
                }
                foreach (MemberInfo mv in tuple)
                {
                    hasRollup = hasRollup | mv.HasCustomRollup;
                }

                /*if (this.InvisibleCoords != null)
                {
                    foreach (MemberInfo mi in this.InvisibleCoords)
                    {
                        hasRollup = hasRollup | mi.HasCustomRollup;
                    }
                }*/

                return hasRollup;
            }
        }
    }
}
